package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.Content;
import com.ericom.service.GenericManager;

public class ContentAction extends BaseAction {

	private GenericManager<Content, Long> contentManager;
	private List contents;
	private Content content;
	private Long id;

	public Content getContent() {
		return content;
	}

	public void setContent(Content content) {
		this.content = content;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setContentManager(GenericManager<Content, Long> contentManager) {
		this.contentManager = contentManager;
	}

	public String list() {
		contents = contentManager.getAll();
		return SUCCESS;
	}

	public List getContents() {
		return contents;
	}

	public String delete() {
		contentManager.remove(content.getId());
		this.saveMessage(getText("content.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			content = contentManager.get(id);
		} else {
			content = new Content();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (content.getId() == null);
		content = contentManager.save(content);

		String key = (isNew) ? "content.added" : "content.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
